//
//  LMNetwokingTools.m
//  LMNetworkingTool
//
//  Created by pc-mac on 16/3/16.
//  Copyright © 2016年 pc-mac. All rights reserved.
//

#import "LMNetwokingTools.h"

///  使用协议来调用父类的私有方法
@protocol LMNetworkingToolDelegate <NSObject>

@optional
// 请求数据
- (NSURLSessionDataTask*)dataTaskWithHTTPMethod:(NSString*)method URLString:(NSString*)URLString parameters:(id)parameters uploadProgress:(nullable void (^)(NSProgress* uploadProgress))uploadProgress downloadProgress:(nullable void (^)(NSProgress* downloadProgress))downloadProgress success:(void (^)(NSURLSessionDataTask*, id))success failure:(void (^)(NSURLSessionDataTask*, NSError*))failure;
@end
///  遵守协议
@interface LMNetwokingTools () <LMNetworkingToolDelegate>

@end
@implementation LMNetwokingTools

- (void)request:(LMRequestMethod)method urlString:(NSString*)urlString parameters:(id)parameters finished:(void (^)(id, NSError*))finished
{
    //  判断请求的类型是'GET'还是'POST'
    NSString* m = method == GET ? @"GET" : @"POST";
    //  发送请求
    // 调用协议的方法(其本质是在调用父类中的私有方法)
    [[self dataTaskWithHTTPMethod:m URLString:urlString parameters:parameters uploadProgress:nil downloadProgress:nil success:^(NSURLSessionDataTask* dataTask, id responseObject) {
        // 请求成功的回调 将数据传出去
        finished(responseObject, nil);
    }
        failure:^(NSURLSessionDataTask* dataTask, NSError* error) {
            //  请求失败的回调
            finished(nil, error);
        }] resume];
}
@end
